package com.dji.GSDemo.GaodeMap.Message;

import static com.dji.frame.util.V_JsonUtil.gson;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


public class MessageHelper {
    public enum MessageType {
        MAVC_REQ_CID,
        MAVC_CID,
        MAVC_STAT,
        MAVC_ACTION,
        MAVC_ARRIVED,
        MAVC_DONE,
    }

    public enum ActionType {
        ACTION_ARM_AND_TAKEOFF,
        ACTION_GO_TO,
        ACTION_GO_BY,
        ACTION_LAND,
    }

    private static MessageHelper instance;
    Gson gson = new Gson();

    private MessageHelper(){};
    public static MessageHelper getInstance() {
        if(instance == null){
            instance = new MessageHelper();
        }
        return instance;
    }

    static List<MessageHandler> observers = new ArrayList<MessageHandler>();
    public static void addObserver(MessageHandler obj){
        observers.add(obj);
    };

    public String getRequestCIDMessage(double lat, double lon){
        Collection messageCollection = new ArrayList();
        messageCollection.add(new SendHead(MessageType.MAVC_REQ_CID.ordinal()));
        messageCollection.add(new QuestCIDMessage(lat, lon));
        return gson.toJson(messageCollection);
    }

    public void parseJson(String json){
        JsonArray jsonArray = JsonParser.parseString(json).getAsJsonArray();
        ReceiveHead receiveHead = gson.fromJson(jsonArray.get(0), ReceiveHead.class);
        if(receiveHead.Type == MessageType.MAVC_REQ_CID.ordinal())
        {
            for(MessageHandler messageHandler:observers){
                messageHandler.onCIDMessage();
            }
        }
        if(receiveHead.Type == MessageType.MAVC_ACTION.ordinal())
        {
            for(MessageHandler messageHandler:observers){
                messageHandler.onActionMessage();
            }
        }
    }

}


class SendHead {
    String Header = "MAVCluster_Drone";
    int Type;
    public SendHead(int type){
        Type = type;
    }
}

class ReceiveHead {
    String Header = "MAVCluster_Monitor";
    int Type;
    public ReceiveHead(){}
    public ReceiveHead(int type){
        Type = type;
    }
}

class QuestCIDMessage{
    double Lat = 181;
    double Lon = 181;
    public QuestCIDMessage(double lat,double lon){
        Lat = lat;
        Lon = lon;
    }
}

class CIDMessage{
    int CID = -1;
    public CIDMessage(int cid){
        CID = cid;
    }
}

class ActionMessage{

}

class ArrivedMessage{
    int CID;
    int Step;
    public ArrivedMessage(int cid,int step){
        CID = cid;
        Step = step;
    }
}


